<template>
    <div>
        <el-form label-width="80px">
            <el-row>
                <el-col :xs="{span:24}" :sm="{span:5}">
                    <el-form-item label="职位名称">
                        <el-input size="small" v-model="jobList.jobName"  placeholder="请输入职位名称"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="{span:24}" :sm="{span:5}">
                    <el-form-item label="所属部门">
                        <el-select size="small"  v-model="jobList.jobDepartment" placeholder="请选择所属部门">
                            <el-option
                            v-for="item in newsAllDepartment"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                            </el-option>
                        </el-select>
                     </el-form-item>
                </el-col>
                <el-col :xs="{span:24}" :sm="{span:5}">
                    <el-form-item label="薪资待遇">
                        <el-input size="small" v-model="jobList.jobSalary"  placeholder="请输入薪资待遇（4000-5000）"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :xs="{span:24}" :sm="{span:12}">
                    <el-form-item label="工作地点">
                        <el-input size="small" v-model="jobList.jobAddress" placeholder="请输入工作地点"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item label="岗位职责">
            </el-form-item>
            <simditor id="jobDuty" ref="jobDuty"
            @change="changeJobDuty"
            :value="jobList.jobDuty"
             >
            </simditor>
            <el-form-item label="任职要求">
            </el-form-item>
            <simditor id="jobRequire" ref="jobRequire"
            @change="changeJobRequire"
            :value="jobList.jobRequire"
             >
            </simditor>
            <div class="list-button">
                <el-button type="primary" @click="subJob()">提交</el-button>
                <router-link to="/manage/job">
                    <el-button>取消</el-button>
                </router-link>
            </div>
        </el-form>
    </div>
</template>
<script>
import  Simditor        from    '@/components/back/manage/Simditor'
import 	Menu 		    from    "@/service/menu-service.js"
const 	_menu 	        =       new Menu();
import 	Job 		    from    "@/service/job-service.js";
const   _job            =new    Job();
export default {
    name:"editJob",
    data(){
        return{
            newsAllDepartment:[],
            jobList:{
                jobId:'',
                jobName:'',
                jobDuty:'',
                jobRequire  :'',
                jobDepartment:'',
                jobSalary:'',
                jobAddress:''
            }
            
        }
    },
    components: {
        Simditor
    },
    created(){
        this.findDepartment();
        // 这里是判断是否是编辑模式，如果有参数的话就是编辑模式，否则就是新增模式
        if(this.$route.params.jobId){
            this.findJobById(this.$route.params.jobId)
        }
    },
    methods:{
        //这是通过ID查找职位
        findJobById(id){
            const loading = this.$loading({
                lock: true,
                text: '加载中...',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            });
            _job.findOneJob({'id':id}).then((res)=>{
                this.jobList.jobId = res.data.job_id;
                this.jobList.jobName = res.data.job_name;
                this.jobList.jobDuty = res.data.job_duty;
                this.jobList.jobRequire = res.data.job_require;
                this.jobList.jobDepartment = res.data.job_department;
                this.jobList.jobSalary = res.data.job_salary;
                this.jobList.jobAddress = res.data.job_address;
                this.setjobRequireValue(res.data.job_require)
                this.setjobDutyValue(res.data.job_duty)
                loading.close();
            },(err)=>{
                console.log(err)
            })
        },
        setjobDutyValue(val){
            this.$refs.jobDuty.setValue(val);
        },
        setjobRequireValue(val){
            this.$refs.jobRequire.setValue(val);
        },
        findDepartment(){
            _menu.menuList({pId:"8"}).then((res)=>{
               this.newsAllDepartment = res.data;
           },(err)=>{
                this.$message.error(err);
           })
        },
        changeJobDuty(val){
            this.jobList.jobDuty = val;
        },
        changeJobRequire(val){
            this.jobList.jobRequire = val;
        },
        subJob(){
            const loading = this.$loading({
                lock: true,
                text: '保存中...',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            });
            _job.saveJob(this.jobList).then((res)=>{
                if(res.data = 1){
                    this.$router.push({path:'/manage/job'});
                }
                loading.close();
            },(err)=>{
                console.log(err);
            })
        }
    }
}
</script>
<style>
@import url("//unpkg.com/element-ui@2.4.11/lib/theme-chalk/index.css");
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}
.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}
.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
.list-button{
    text-align: center;
    margin: 20px;
}
</style>
